package com.CMC.yhdeng;

import java.io.IOException;
import java.util.ArrayList;

import android.util.JsonReader;
import android.util.Log;

public class QuizContent extends UnitBase {
	public class QuizExtra {
		public Boolean upper, lower;
		public String hint;
	}

	public ArrayList<Integer> red;
	protected ArrayList<QuizExtra> extraInfo;
	protected String filename;
	protected String answer, a_file, is_practice, is_test;

	protected enum QCState {
		UNANSWERED, CORRECT, WRONG
	};

	protected QCState answerState = QCState.UNANSWERED;

	public void read(JsonReader reader, String sep) throws IOException {
		reader.beginObject();
		id = readStringFromTag(reader, "quiz_id");

		String sTemp = readStringFromTag(reader, "upper");
		ArrayList<String> sSepREs=null;
		if (sTemp != null && sTemp.length()>0) {
			sSepREs = UnitBase.seperateStringByPipe(sTemp);
			int nSepRes=sSepREs.size();
			if (nSepRes > 0) {
				extraInfo = new ArrayList<QuizExtra>(nSepRes);
				int j=0;
				for(int i=0; i<nSepRes;++i)
				{
					QuizExtra qe=new QuizExtra();
					String string=sSepREs.get(j++);
					qe.upper = string.compareTo("1") == 0 ? true : false;
					extraInfo.add(qe);
				}
				
			}
		}
		title = readStringFromTag(reader, "q_text");
		if(title.compareTo("question")==0)
		{
			Log.d("aa","ok");
		}
		if (sSepREs!=null && sSepREs.size() > 0) {
			// lower
			sTemp = readStringFromTag(reader, "lower");

			sSepREs =UnitBase.seperateStringByPipe(sTemp);

			int i = 0;
			for (QuizExtra qe : extraInfo) {
				String string=sSepREs.get(i++);
				qe.lower = string.compareTo("1") == 0 ? true : false;
			}
			
			// extra
			sTemp = readStringFromTag(reader, "extra");
			sSepREs = UnitBase.seperateStringByPipe(sTemp);
			i = 0;
			for (QuizExtra qe : extraInfo) {
				String string=sSepREs.get(i++);
				qe.hint = string;
			}
		}else{
//			reader.nextName();
//			reader.nextNull(); //lower
//			reader.nextName();
//			reader.nextNull(); //extra
			
			reader.skipValue();
			reader.skipValue();
			reader.skipValue();
			reader.skipValue();
		}
		filename = readStringFromTag(reader, "q_file");
		answer = readStringFromTag(reader, "a_text");
		a_file = readStringFromTag(reader, "a_file");
		is_practice = readStringFromTag(reader, "is_practice");
		is_test = readStringFromTag(reader, "is_test");
		reader.endObject();

		//parse ^
		char[] aryChr=title.toCharArray();
		red=new ArrayList<Integer>(2);
		int redPos=-1, pos=0;
		for (char ch : aryChr) {
			if(ch=='^') 
				red.add(++redPos, pos);
			pos++;
		}
		

		//filter ^
		if(red.size()>0)
		{
			StringBuilder sb=new StringBuilder(aryChr.length-2);
			pos=0;
			redPos=0;
			for(char ch:aryChr)
			{
				if(redPos<2 && pos==red.get(redPos))
				{
					redPos++;
				}else{
					sb.append(ch);
				}
				pos++;
			}
			title=sb.toString();
		}
		
		if(sSepREs!=null && redPos!=-1)
		{
			//開始處理上下標的位置
			pos=0;
			redPos=0;
			for(Integer rpos:red)
			{
				int endP=extraInfo.size()-redPos-1;
				for(int i=rpos; i< endP;++i)
				{
					QuizExtra qe=extraInfo.get(i);
					QuizExtra qe1=extraInfo.get(i+1);
					qe.lower=qe1.lower;
					qe.upper=qe1.upper;
					qe.hint=qe1.hint;
				}
				redPos++;
			}
			
		}
	}
	// public String getSoundUrl()
	// {
	// return String.format("http://www.urjotter.com/files/audio/2/%s",
	// filename);
	// }
}
